package com.jaydenxiao.common.commonwidget;

import android.content.Context;
import android.content.res.TypedArray;
import android.graphics.Color;
import android.graphics.drawable.Drawable;
import android.os.Build;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.ViewGroup;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.jaydenxiao.common.R;


/**
 * Created by admin on 2017-04-11.
 */

public class TopBar extends RelativeLayout {

    private TextView titleTV;
    private Button leftBt,rightBt;
    private ImageView leftImageBt,rightImageBt;

    private String leftBtText,rightBtText,titleText;
    private float leftBtTextSize,rightBtTextSize,titleTVTextSize;
    private int leftBtTextColor,rightBtTextColor,titleTVTextColor;
    private Drawable leftImageBtDrawable,rightImageBtDrawable;
    private boolean isVisibleLeftImage,isVisibleLeftText,isVisibleRightImage,isVisibleRightText;


    public TopBar(Context context, AttributeSet attrs) {
        super(context, attrs);
        TypedArray typedArray=context.obtainStyledAttributes(attrs, R.styleable.TopBar);
        getAttribute(typedArray);
        initView(context);

    }

    private void getAttribute(TypedArray typedArray) {
        leftBtText=typedArray.getString(R.styleable.TopBar_leftButtonText);
        leftBtTextSize=typedArray.getDimension(R.styleable.TopBar_leftButtonTextSize,16);
        leftBtTextColor=typedArray.getColor(R.styleable.TopBar_leftButtonTextColor, Color.WHITE);
        isVisibleLeftImage=typedArray.getBoolean(R.styleable.TopBar_isVisibleLeftImage, false);
        isVisibleLeftText=typedArray.getBoolean(R.styleable.TopBar_isVisibleLeftText, false);


        rightBtText=typedArray.getString(R.styleable.TopBar_rightButtonText);
        rightBtTextSize=typedArray.getDimension(R.styleable.TopBar_rightButtonTextSize,16);
        rightBtTextColor=typedArray.getColor(R.styleable.TopBar_rightButtonTextColor, Color.WHITE);
        isVisibleRightImage=typedArray.getBoolean(R.styleable.TopBar_isVisibleRightImage, false);
        isVisibleRightText=typedArray.getBoolean(R.styleable.TopBar_isVisibleRightText, false);

        titleText=typedArray.getString(R.styleable.TopBar_titleText);
        titleTVTextColor=typedArray.getColor(R.styleable.TopBar_titleTextColor,Color.WHITE);
        titleTVTextSize=typedArray.getDimension(R.styleable.TopBar_titleTextSize,18);

        leftImageBtDrawable=typedArray.getDrawable(R.styleable.TopBar_leftImageResource);
        rightImageBtDrawable=typedArray.getDrawable(R.styleable.TopBar_rightImageResource);

        typedArray.recycle();

    }

    private void initView(Context context) {

        leftBt=new Button(context);
        leftBt.setText(leftBtText);
        leftBt.setTextSize(leftBtTextSize);
        leftBt.setTextColor(leftBtTextColor);
        leftBt.setGravity(Gravity.LEFT|Gravity.CENTER_VERTICAL);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            leftBt.setBackground(null);
        }
        leftBt.setVisibility(isVisibleLeftText?VISIBLE:GONE);

        rightBt=new Button(context);
        rightBt.setText(rightBtText);
        rightBt.setTextSize(rightBtTextSize);
        rightBt.setTextColor(rightBtTextColor);
        if (Build.VERSION.SDK_INT >= Build.VERSION_CODES.JELLY_BEAN) {
            rightBt.setBackground(null);
        }
        rightBt.setGravity(Gravity.RIGHT|Gravity.CENTER_VERTICAL);
        rightBt.setVisibility(isVisibleRightText?VISIBLE:GONE);

        titleTV=new TextView(context);
        titleTV.setText(titleText);
        titleTV.setTextSize(titleTVTextSize);
        titleTV.setTextColor(titleTVTextColor);

        leftImageBt=new ImageView(context);
        leftImageBt.setImageDrawable(leftImageBtDrawable);
        leftImageBt.setVisibility(isVisibleLeftImage?VISIBLE:GONE);

        rightImageBt=new ImageView(context);
        rightImageBt.setImageDrawable(rightImageBtDrawable);
        rightImageBt.setVisibility(isVisibleRightImage?VISIBLE:GONE);

        LayoutParams leftParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        leftParams.addRule(RelativeLayout.ALIGN_PARENT_LEFT,TRUE);
        leftParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        addView(leftBt,leftParams);
        addView(leftImageBt,leftParams);

        LayoutParams rightParams=new LayoutParams(LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        rightParams.addRule(RelativeLayout.ALIGN_PARENT_RIGHT,TRUE);
        rightParams.addRule(RelativeLayout.CENTER_VERTICAL,TRUE);
        addView(rightBt,rightParams);
        addView(rightImageBt,rightParams);

        LayoutParams titleParams=new LayoutParams(ViewGroup.LayoutParams.WRAP_CONTENT, ViewGroup.LayoutParams.WRAP_CONTENT);
        titleParams.addRule(RelativeLayout.CENTER_IN_PARENT,TRUE);
        addView(titleTV,titleParams);
    }


    public TextView getLeftTV() {
        return leftBt;
    }

    public TextView getRightTV() {
        return rightBt;
    }

    public TextView getTitleTV() {
        return titleTV;
    }

    public ImageView getLeftImageBt() {
        return leftImageBt;
    }

    public ImageView getRightImageBt() {
        return rightImageBt;
    }
}
